package org.liang.able.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.liang.able.service.IEmpImpOrExpService;
import org.liang.base.utils.AjaxResult;
import org.liang.system.domain.Employee;
import org.liang.system.domain.EmployeeDTO;
import org.liang.system.service.IEmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import java.util.*;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/excellList")
@Api(value = "导入导出管理接口",description = "导入导出")
public class EmpImpOrExpController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IEmpImpOrExpService empImpOrExpService;

    //导入
    @ApiOperation(value = "导入")
    @PostMapping("/import")
    public AjaxResult upload(@RequestParam("file") MultipartFile multipartFile) throws Exception{
        //new一个模板
        ImportParams params = new ImportParams();


        //开始读取，并用list接收，方便处理
        List<EmployeeDTO> result = ExcelImportUtil.importExcel(multipartFile.getInputStream(), EmployeeDTO.class, params);

        String check = "^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$";
        List<EmployeeDTO> right = new ArrayList<>();
        List<EmployeeDTO> bad = new ArrayList<>();
        for(EmployeeDTO e:result){
            if(Pattern.matches(check, e.getEmail())){
                right.add(e);
                continue;
            }
            bad.add(e);
        }
        employeeService.batchInsert(right);
        Map<String, Object> map = new HashMap<>();
        map.put("upSuccess",right);
        map.put("dwFail",bad);
        return AjaxResult.success("上传成功！",map);
    }

    //导出
    @ApiOperation(value = "浏览器带参导出")
    @GetMapping("/export/{key}")
    public void export(@PathVariable("key") String key,HttpServletResponse response) throws Exception{

        List<EmployeeDTO> result = employeeService.queryAll(key);
        empImpOrExpService.downLoad(result,response);
    }

    //导出
    @ApiOperation(value = "浏览器无参导出")
    @GetMapping("/export")
    public void export2(HttpServletResponse response) throws Exception{

        List<EmployeeDTO> result = employeeService.queryAll();
        empImpOrExpService.downLoad(result,response);
    }

    //导出
    @ApiOperation(value = "错误信息导出")
    @GetMapping("/exportFail/{list}")
    public void exportFail(@PathVariable("list") List<EmployeeDTO> list, HttpServletResponse response) throws Exception{

        empImpOrExpService.outPut(list,response);
    }

}
